<!-- 移动签批sign >紧急重要公文列表 -->
<template>
	<view>
		<van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="load_more"
				offset="20" :error.sync="error" error-text="请求失败，点击重新加载"
				:immediate-check="immediateCheck">
      <van-checkbox-group v-model="result">
        <van-swipe-cell  v-for="(item,index) in list" :right-width="80">
          <van-cell center :title="item.title"
              :label="'公文类型：' + item.bizType" class="content"
              @click="toDetail(item.extendIdForRelate,item.proInstId)"  value-class="text-sm" >
            <template #extra>
              <span v-if="item.gwStatus != null && item.gwStatus != ''" class="content-extra">
                <i class="iconfont icon-dian"></i>{{item.gwStatus}}
              </span>
            </template>
            <template #title>
              <span class="content-title">
                <i :class="iconType(item.bizType)"></i>{{item.title}}
              </span>
            </template>
          </van-cell>
          <template #right>
            <van-checkbox :name="item.bizId" :key="item.bizId" @click.native="collectionChange(item,index)">
              <text v-if="isChecked(item.bizId)">{{ collectionState }}</text>
              <text v v-else>{{ inCollectionState }}</text>
              <template #icon="props">
                <img class="img-icon" :src="isChecked(item.bizId) ? activeIcon : inactiveIcon" />
              </template>
            </van-checkbox>
          </template>
        </van-swipe-cell>
      </van-checkbox-group>
		</van-list>
	</view>
</template>

<script>
	export default {
		props:{
			todoTab: {
				type: String
			},
			finishStatus: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
        //收藏
        result:[],
        collectionState:'已收藏',
        inCollectionState:'收藏',
        activeIcon: require('@/static/imgs/icon/icon-collection-active.png'),
        inactiveIcon: require('@/static/imgs/icon/icon-collection.png'),

				configCode:'',    // 公文类型
				list: [],
				loading: false,   // 加载状态
				finished: false,  // 是否加载
				refreshing: false,// 是否刷新
				error: false,     // 错误状态
				pageNum: 1,   		// 页数
				pageSize: 10, 		// 条数
				wordType: '', 		// 类型
				immediateCheck: true, // 是否在初始化时立即执行滚动位置检查　
			}
		},
		watch:{
			todoTab: {
				handler(nval, oval) {
					this.wordType = nval
					this.onRefresh()
				},
				immediate: true,
				deep: true
			},
			finishStatus: {
				handler(nval, oval) {
					this.finished = nval
				},
				immediate: true,
				deep: true
			}
		},
		mounted() {

		},
		methods: {
      // 判断是否选中
      isChecked(bizId) {
        let coll = this.result.filter(t => t == bizId)
        if(coll != null && coll.length > 0){
          return true
        } else {
          return false
        }
      },

      //收藏状态
      collectionChange(item, index){
        let flag = this.isChecked(item.bizId)
        if(flag){
          // console.log('取消收藏')
          // 取消收藏
          this.cancelcCollection(item,index)
        }else{
          // console.log('收藏')
          // this.result.push(item.bizId)
          // 调用收藏接口
          this.addCollection(item,index)
        }

      },
      //获取左侧收藏树,顶级节点id默认为‘root'
      /*getCollectionDir(){
        this.$http({
          url: this.$http.adornUrl(`/caac/oa/common/dir/getCollectionDir`),
          method: 'get',
          params: this.$http.adornParams({
            'systemKey': this.cache.get('userInfo').systemKey,
            'systemFlag': this.cache.get('userInfo').systemFlag,
          })
        }).then(({data}) => {

        })
      },*/
      //添加收藏
      addCollection(item,index){
        this.$http({
          url: this.$http.adornUrl(`/caac/oa/common/collection/addcollection`),
          method: 'get',
          params: this.$http.adornParams({
            'systemKey': this.cache.get('userInfo').systemKey,
            'systemFlag': this.cache.get('userInfo').systemFlag,
            'gwId':item.proInstId,
            'dirId':'root',
            'gwTitle':item.title,
            'gwType':item.code,
            'bizId':item.bizId
          })
        }).then(({data}) => {
          //{"code":200,"data":{"message":"收藏成功！"},"message":null}
          if(data.code == 200){
            this.$toast('收藏成功')
          }else{
            this.$toast(data.data.message)
          }
        })
      },
      //取消收藏
      cancelcCollection(item,index){
        this.$http({
          url: this.$http.adornUrl(`/caac/oa/common/collection/cancelcollection`),
          method: 'delete',
          params: this.$http.adornParams({
            'systemKey': this.cache.get('userInfo').systemKey,
            'systemFlag': this.cache.get('userInfo').systemFlag,
            'gwId':item.proInstId,
          })
        }).then(({data}) => {
          //{"code":200,"data":"取消收藏成功!","message":null}
          if(data.code == 200){
            this.$toast('取消收藏成功')
          }else{
            this.$toast(data.data.message)
          }
        })
      },

      // 下拉刷新
			onRefresh() {
				// 先清空数据
				this.list = []
				// 将没有更多的状态改成false
				this.finished = false
				// 默认加载第一页
				this.pageNum = 1
				this.getList()
				this.refreshing = false
			},

			// 上拉加载
			load_more() {
				// 页数设置
				if( this.list.length == 0){
					this.pageNum = 1
				} else {
					this.pageNum++
				}
				// 将下拉刷新停止
				this.refreshing = false;
				setTimeout(() => {
					this.getList()
				}, 500)
			},

			// 获取收文数据
			getList() {
				// 加载状态
				this.loading = true
				this.$http({
				  url: this.$http.adornUrl(`/caac/oa/mywork/MytodoWorks`),
				  method: 'get',
				  params: this.$http.adornParams({
				    'todoTab': this.wordType,
				    'page': this.pageNum,
				    'rows': this.pageSize,
				    'systemKey': this.cache.get('userInfo').systemKey,
				    'systemFlag': this.cache.get('userInfo').systemFlag,
				  })
				}).then(({data}) => {
					if(data.code == 200) {
						if(data.data != null && JSON.stringify(data.data) != '{}') {
							if(this.pageNum == 1) {
								this.list = data.data.rows
							} else {
								data.data.rows.forEach( t => this.list.push(t))
							}
              //收藏
              this.result=[]
              this.list.forEach((item)=>{
                if(item.isCollect == '1'){
                  this.result.push(item.bizId)
                }
              })
							// 判断当前页是否为最后一页
							if((this.pageNum*this.pageSize) > data.data.total) {
								this.finished = true
							}
						} else {
              this.finished = true
            }
					} else {
						this.error = true
					}
				}).catch((err) => {
					this.error = true
				}).finally(() => {
					// 关闭加载状态
					this.loading = false
				})

			},



			//跳转详情code:发文类型，proInstId
			toDetail(extendIdForRelate,proInstId){
				// 会签件--hqj，发文（函）--glj_fw，呈批件--glj_cpj，发电--fd_hbglj，发电抄阅--hbgljfd，收文--glj_sw
				var _this = this
				this.$http({
				  url: this.$http.adornUrl(`/caac/oa/comm/comm/openwfneededdata`),
				  method: 'get',
				  params: this.$http.adornParams({
					  'id': extendIdForRelate,//发文详情返回的fwId
					  'proInstId': proInstId,//发文详情返回的proInstId
					 'systemKey': this.cache.get('userInfo').systemKey,//固定值
					 'systemFlag': this.cache.get('userInfo').systemFlag,//固定值
				  })
				}).then(({data}) => {
					if(data.code==200){
						_this.configCode = data.data.configCode
						console.log("公文类型==>"+_this.configCode)
						if(_this.configCode != ''){
              if(_this.configCode =='jgj_fw'){//发文（函）
                uni.navigateTo({
                  url:"/pages/jgj/send/index?proInstId="+proInstId
                })
              }else if(_this.configCode == 'hqj'){//TODO 公文流转发文（函）-会签件
                uni.navigateTo({
                  url:"/pages/jgj/send/index?proInstId="+proInstId
                })
              }else if(_this.configCode=='hbjgj_cpj'){//glj_cpj--呈批件
                //跳转呈批件
                uni.navigateTo({
                  url:"/pages/jgj/submit/index?proInstId="+proInstId
                })

              }else if(_this.configCode=='hbjgjgwfd'){// fd_hbglj--发电
                //跳转发电
                uni.navigateTo({
                  url:"/pages/jgj/sendMsg/index?proInstId="+proInstId
                })

              }else if(_this.configCode=='hbjgjfd'){//hbgljfd--发电抄阅
                //跳转发电抄阅
                uni.navigateTo({
                  url:"/pages/jgj/sendMsgCopy/index?proInstId="+proInstId
                })
              }else if(_this.configCode=='hbjgjsd'){//hbgljfd--收电批办
                //跳转收电批办
                uni.navigateTo({
                  url:"/pages/jgj/receiveMsgApproval/index?proInstId="+proInstId
                })
              }else if(_this.configCode == 'hbjgjsw'){// glj_sw收文
                // 跳转收文详情
                uni.navigateTo({
                  url:"/pages/jgj/receive/index?proInstId="+proInstId + "&from=db"
                })
              }else{
                _this.$toast('其他发文类型正在接入中，请先体验发文类型公文')

              }
              // this.common.toJgj(_this.configCode,proInstId,'')
						}
					}else{
						this.$toast(data.message)
					}

				})

				// this.openwfneededdata(extendIdForRelate,proInstId)


			},

			/**
			 * 通过这个接口获取公文类型，取configCode 进行公文类型判断
			 * @param {Object} extendIdForRelate
			 * @param {Object} proInstId
			 * {
				  "code": 200,
				  "data": {
					"bizId": "64b4a5ceacf94e739710f85a54cbca56",
					"proInstId": "e48da66a1c06433abd40e5a10f127878",
					"configId": "e04cb1a4a24e4c358a75ce2fef26fef8",
					"proDirId": "f5128cec3df2408da8cb9d9047051a5d",
					"actInstId": "554752e0bc7d4c6396e21671427b1af3",
					"actDefId": "hqjsr",
					"workitemId": "c77ade0c18b74ed4814ae2a692135742",
					"state": null,
					"isClosed": "2",
					"configCode": "glj_fw"
				  },
				  "message": null
				}
			 */
			openwfneededdata(extendIdForRelate,proInstId){
				// extendIdForRelate = 'abd03f22703a4d2da09b17d9b119b405'//TODO 测试用，写死
				// proInstId='0708bdf2d218451d8da6c01bdbaf2ade'
				var _this = this
				this.$http({
				  url: this.$http.adornUrl(`/caac/oa/comm/comm/openwfneededdata`),
				  method: 'get',
				  params: this.$http.adornParams({
					  'id': extendIdForRelate,//发文详情返回的fwId
					  'proInstId': proInstId,//发文详情返回的proInstId
					 'systemKey': this.cache.get('userInfo').systemKey,//固定值
					 'systemFlag': this.cache.get('userInfo').systemFlag,//固定值
				  })
				}).then(({data}) => {
					if(data.code==200){
						this.configCode = data.data.configCode
					}

				})
			},
      // 公文标签
      iconType(type) {
        let icon = 'iconfont icon-shoufawen content-icon'
        if (type == '发文') {
          icon = 'iconfont icon-wj-fswj content-icon'
        }
        if (type == '收文') {
          icon = 'iconfont icon-shouwen content-icon'
        }
        if (type == '发电') {
          icon = 'iconfont icon-fasong content-icon'
        }
        if (type == '发电抄阅') {
          icon = 'iconfont icon-chuansong content-icon'
        }
        if (type == '收电') {
          icon = 'iconfont icon-accept content-icon'
        }
        if (type == '收电批办') {
          icon = 'iconfont icon-jieshou content-icon'
        }
        if (type == '呈批件') {
          icon = 'iconfont icon-chengpijian content-icon'
        }
        return icon
      },

		},
	}
</script>

<style scoped>
	.van-cell__title {
		min-width: 50%;

		span {
			display: inline-block;
			text-align: left;
			word-break: break-all;
		}
	}
  .content {
    padding: 20rpx 30rpx;
  }
  .content-extra {
    color: #0081ff;
    font-size: 24rpx;
  }
  .content-title {
    padding: 18rpx 0px;
  }
  .content-icon {
    margin-right: 10rpx;
  }
  /deep/ .van-cell__label {
    margin-left: 40rpx;
  }
  /*收藏样式*/
  /deep/  .van-swipe-cell__right{
    display: flex;
    align-items: center;
  }
  .img-icon {
    height: 40rpx;
  }
  /*收藏样式*/
</style>
